package org.petstore.web.servlet;

import org.petstore.domain.Account;
import org.petstore.domain.CartItem;
//import org.petstore.domain.User;
import org.petstore.service.CartService;
import org.petstore.service.LogService;
import org.petstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

@WebServlet(name = "UpdateCartServlet")
public class UpdateCartServlet extends HttpServlet {

    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";
    private Account account;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String itemId = request.getParameter("itemId");
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        HttpSession session = request.getSession();
        account = (Account) session.getAttribute("account");
        String username = account.getUsername();
        CartService cartService = new CartService();
        cartService.updateItemByItemIdAndQuantity(username, itemId, quantity);
        List<CartItem> cart = cartService.selectItemByUsername(username);
        session.setAttribute("cart", cart);
        request.getRequestDispatcher(VIEW_CART).forward(request, response);

        if (account != null) {
            HttpServletRequest httpRequest = request;
            String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + " 更新购物车商品数量";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        /*
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String itemId = paramNames.nextElement();
            int quantity = Integer.parseInt(request.getParameter(itemId));

            if (quantity != 0) {
                cartService.updateItemByItemIdAndQuantity(username, itemId, quantity);
                if (account != null) {
                    HttpServletRequest httpRequest = request;
                    String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + strBackUrl + " 更新购物车商品数量";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }
            }
        }
        */
    }
}
